﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Antlr.Runtime;
using Antlr.Runtime.Tree;
using CFrontEnd;

namespace CCompiler
{
    public class Compiler
    {
        private string _sourceFile = null;

        public Compiler(string sourceFile)
        {
            if (sourceFile == null)
                throw new ArgumentNullException("sourceFile");

            _sourceFile = sourceFile;
        }

        public string Compile()
        {
            System.Diagnostics.Debug.WriteLine("Start to compile file: " + _sourceFile);

            var fileStream = new ANTLRFileStream(_sourceFile);
            var lexer = new cLexer(fileStream);
            var tokenStream = new CommonTokenStream(lexer);
            var parser = new cParser(tokenStream);
            var ast = parser.compileUnit().Tree;
            var treeStream = new CommonTreeNodeStream(ast);
            var treeParser = new CTreeParser(treeStream);
            treeParser.compileUnit();

            string astString = DumpAst(0, ast);

            return astString;
        }

        private string DumpAst(int indentLevel, ITree ast)
        {
            return ast.ToStringTree();
        }
    }
}
